package com.liang.test;

import java.util.Arrays;
import java.util.Random;
import java.util.Scanner;

/**
 * @author Liangyaoyun
 */
public class TractorGame {
    //该方法用来判断手牌
    public static int panDuan(int[] card) {
        int sorce = 0;
        //豹子
        if (card[0] == card[1] && card[1] == card[2]) {
            sorce = 400000 + card[0] * 100;
        }//顺子
        else if (card[2] == card[1] + 1 && card[1] == card[0] + 1) {
            sorce = 300000 + card[2] * 100;
        }//对子
        else if (card[0] == card[1] || card[1] == card[2]) {
            if (card[0] == card[1]) {
                sorce = 200000 + card[0] * 100 + card[2];
            } else if (card[1] == card[2]) {
                sorce = 200000 + card[2] * 100 + card[0];
            }
        } //单牌
        else {
            sorce = card[2] * 10000 + card[1] * 100 + card[0] * 10;
        }
        return sorce;
    }
    //炸金花游戏
    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        // 1. 准备52张牌
        // 2. 洗牌
        // 3. 发牌
        // 4. 码牌
        // 5. 分析
        // 6. 下注
        // 7. 开牌
        //准备52张牌
        int[] allCards = {2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4,
                5, 5, 5, 5, 6, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 8,
                9, 9, 9, 9, 10, 10, 10, 10, 11, 11, 11, 11, 12, 12, 12, 12,
                13, 13, 13, 13, 14, 14, 14, 14};
        int[] Mycards = {0, 0, 0};
        int[] Yourcards = {0, 0, 0};
        for (int i = 0; i < 1000; i++) {
            //洗牌
            Random r = new Random();
            int a = r.nextInt(52);
            int b = r.nextInt(52);
            int t = 0;
            t = allCards[a];
            allCards[a] = allCards[b];
            allCards[b] = t;
        }
        for (int i = 0; i < 3; i++) {
            //发牌
            Mycards[i] = allCards[i];
            Yourcards[i] = allCards[i + 3];
        }
        //摆牌
        Arrays.sort(Mycards);
        Arrays.sort(Yourcards);
        int MyScore = 0;
        int YourScore = 0;
        int MyMoney = 10000;
        int YourMoney = 10000;
        while (YourMoney > 0 && MyMoney > 0) {
            Arrays.sort(Mycards);
            Arrays.sort(Yourcards);
            System.out.printf("我的手牌是：");
            System.out.println(show(Mycards));
            System.out.printf("请下注：");
            int inMoney = scanner.nextInt();;
            MyScore = panDuan(Mycards);
            YourScore = panDuan(Yourcards);
            System.out.printf("你的手牌是：");
            System.out.printf(show(Yourcards));
            System.out.println();
            if (MyScore > YourScore) {
                System.out.println("我赢了");
                YourMoney -= inMoney;
                MyMoney += inMoney;
                System.out.printf("我的钱剩下%d\n", MyMoney);
                System.out.printf("你的钱剩下%d\n", YourMoney);
            } else if (MyScore == YourScore) {
                System.out.println("平局\n");
            } else {
                YourMoney += inMoney;
                MyMoney -= inMoney;
                System.out.println("你赢了\n");
                System.out.printf("我的钱剩下%d\n", MyMoney);
                System.out.printf("你的钱剩下%d\n", YourMoney);
            }
            System.out.println();
            for (int i = 0; i < 1000; i++) {
                //洗牌
                Random r = new Random();
                int a = r.nextInt(52);
                int b = r.nextInt(52);
                int t = 0;
                t = allCards[a];
                allCards[a] = allCards[b];
                allCards[b] = t;
            }
            for (int i = 0; i < 3; i++) {
                //发牌
                Mycards[i] = allCards[i];
                Yourcards[i] = allCards[i + 3];
            }
        }
    }
    //转换手牌
    public static String show(int[] cards) {
        return Arrays.toString(cards).replace("11", "J")
                .replace("12", "Q")
                .replace("13", "K")
                .replace("14", "A");
    }
}
